/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.starmeep.config;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author CARLOS
 */
public class ConfigStarCluster {
ArrayList<Cluster> clusteres;
ArrayList<EBSVolume> volumenes;
private String awsKey,secretKey,account,pluginName,pluginClass,pluginParameterNumber,pluginOtherParameterNumber;
public static final String pathConfigurationStarCluster="/root/.starcluster/config";
   public  ConfigStarCluster()
   {}

    public void setClusteres(ArrayList<Cluster> clusteres) {
        this.clusteres = clusteres;
    }

    public void setVolumenes(ArrayList<EBSVolume> volumenes) {
        this.volumenes = volumenes;
    }

    public ArrayList<Cluster> getClusteres() {
        return clusteres;
    }

    public ArrayList<EBSVolume> getVolumenes() {
        return volumenes;
    }
    public void setAccount(String account) {
        this.account = account;
    }

    public void setAwsKey(String awsKey) {
        this.awsKey = awsKey;
    }


    public void setPluginClass(String pluginClass) {
        this.pluginClass = pluginClass;
    }

    public void setPluginName(String pluginName) {
        this.pluginName = pluginName;
    }

    public void setPluginOtherParameterNumber(String pluginOtherParameterNumber) {
        this.pluginOtherParameterNumber = pluginOtherParameterNumber;
    }

    public void setPluginParameterNumber(String pluginParameterNumber) {
        this.pluginParameterNumber = pluginParameterNumber;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public String getAccount() {
        return account;
    }

    public String getAwsKey() {
        return awsKey;
    }


    public String getPluginClass() {
        return pluginClass;
    }

    public String getPluginName() {
        return pluginName;
    }

    public String getPluginOtherParameterNumber() {
        return pluginOtherParameterNumber;
    }

    public String getPluginParameterNumber() {
        return pluginParameterNumber;
    }

    public String getSecretKey() {
        return secretKey;
    }
    public HashMap  toHashMap() {
        HashMap configCurrent=new HashMap();
        configCurrent.put("keyLocation", this.getClusteres().get(0).clusterKeyLocation);
        configCurrent.put("keyName", this.getClusteres().get(0).clusterKeyName);
        configCurrent.put("cbSizeTotal", this.getClusteres().get(0).clusterSize);
        configCurrent.put("cbShell", this.getClusteres().get(0).clusterShell);
        configCurrent.put("cbZone", this.getClusteres().get(0).getClusterZone());
        if(this.getVolumenes()!=null && this.getVolumenes().size()!=0)
        {
            configCurrent.put("ebsVolumeId", this.getVolumenes().get(0).getVolumeID());
            configCurrent.put("ebsDevice", this.getVolumenes().get(0).getVolumeDevice());
            configCurrent.put("ebsPartition", this.getVolumenes().get(0).getVolumePartition());
            configCurrent.put("ebsMountPath", this.getVolumenes().get(0).getVolumePathMount());
        }

        configCurrent.put("accountNumber", this.getAccount());
        configCurrent.put("awsKey", this.getAwsKey());
        configCurrent.put("secretAws", this.getSecretKey());
    
        return configCurrent;
    }

    public static ConfigStarCluster readConfigFromFile(String path) {
                ConfigStarCluster config=new ConfigStarCluster();
                Cluster cluster=new Cluster();
                EBSVolume volume= new EBSVolume();
		try {
			/****** Crea un BufferedReader a partir de un archivo **********/
			File f = new File(path);
                        String line="";
                        BufferedReader entrada = new BufferedReader( new FileReader( f ) );
                        ArrayList<Cluster> clusters=new ArrayList<Cluster>();
                        ArrayList<EBSVolume> volumes=new ArrayList<EBSVolume>();
                         if ( f.exists() )
                         {
                             while ((line = entrada.readLine())!=null) {
                                if(line.contains("AWS_ACCESS_KEY_ID ="))
                                {
                                    line=line.replaceAll("AWS_ACCESS_KEY_ID =", "");
                                    config.setAwsKey(line);
                                }
                                if(line.contains("AWS_SECRET_ACCESS_KEY ="))
                                {
                                    line=line.replaceAll("AWS_SECRET_ACCESS_KEY =", "");
                                    config.setSecretKey(line);
                                }
                                if(line.contains("AWS_USER_ID="))
                                {
                                    line=line.replaceAll("AWS_USER_ID=", "");
                                    config.setAccount(line);
                                }
                                if(line.contains("KEYNAME = "))
                                {
                                    line=line.replaceAll("KEYNAME = ", "");
                                    cluster.setClusterKeyName(line);
                                }
                                if(line.contains("KEY_LOCATION="))
                                {
                                    line=line.replaceAll("KEY_LOCATION=", "");
                                    cluster.setClusterKeyLocation(line);
                                }
                                if(line.contains("VOLUME_ID ="))
                                {
                                    line=line.replaceAll("VOLUME_ID =", "");
                                    volume.setVolumeID(line);
                                }
                                if(line.contains("DEVICE ="))
                                {
                                    line=line.replaceAll("DEVICE =","");
                                    volume.setVolumeDevice(line);
                                }
                                if(line.contains("PARTITION ="))
                                {
                                    line=line.replaceAll("PARTITION =","");
                                    volume.setVolumePartition(line);
                                }
                                if(line.contains("MOUNT_PATH ="))
                                {
                                    line=line.replaceAll("MOUNT_PATH =","");
                                    volume.setVolumePathMount(line);
                                }
                                if(line.contains("CLUSTER_SIZE = "))
                                {
                                    line=line.replaceAll("CLUSTER_SIZE = ", "");
                                    cluster.setClusterSize(line);
                                }
                                if(line.contains("CLUSTER_SHELL = "))
                                {
                                    line=line.replaceAll("CLUSTER_SHELL = ", "");
                                    cluster.setClusterShell(line);
                                }
                                if(line.contains("MASTER_IMAGE_ID = "))
                                {
                                    line=line.replaceAll("MASTER_IMAGE_ID = ", "");
                                    cluster.setClusterMasterImage(line);
                                }
                                if(line.contains("MASTER_INSTANCE_TYPE = "))
                                {
                                    line=line.replaceAll("MASTER_INSTANCE_TYPE = ", "");
                                    cluster.setClusterMasterType(line);
                                }
                                if(line.contains("NODE_IMAGE_ID = "))
                                {
                                    line=line.replaceAll("NODE_IMAGE_ID = ", "");
                                    cluster.setClusterNodeImage(line);
                                }
                                if(line.contains("NODE_INSTANCE_TYPE = "))
                                {
                                    line=line.replaceAll("NODE_INSTANCE_TYPE = ", "");
                                    cluster.setClusterNodeType(line);
                                }
                                if(line.contains("AVAILABILITY_ZONE ="))
                                {
                                    line=line.replaceAll("AVAILABILITY_ZONE =", "");
                                    cluster.setClusterZone(line);
                                }
                             }
                             clusters.add(cluster);
                             if(volume.getVolumeID()!=null)
                                volumes.add(volume);
                             config.setClusteres(clusters);
                             config.setVolumenes(volumes);
                         }
                         else
                            return null;
		}
                catch(Exception e){
                    e.getMessage();
                    return null;
                }
                return config;
    }
        public String  toString() {
        String configCurrent;
        configCurrent=this.getClusteres().get(0).clusterKeyLocation+";";
        configCurrent+=this.getClusteres().get(0).clusterKeyName+";";
        configCurrent+=this.getClusteres().get(0).clusterSize+";";
        configCurrent+=this.getClusteres().get(0).clusterShell+";";
        configCurrent+=this.getClusteres().get(0).getClusterZone()+";";
        if(this.getVolumenes()!=null && this.getVolumenes().size()!=0)
        {
            configCurrent+=this.getVolumenes().get(0).getVolumeID()+";";
            configCurrent+=this.getVolumenes().get(0).getVolumeDevice()+";";
            configCurrent+=this.getVolumenes().get(0).getVolumePartition()+";";
            configCurrent+=this.getVolumenes().get(0).getVolumePathMount()+";";
        }

        configCurrent+=this.getAccount()+";";
        configCurrent+=this.getAwsKey()+";";
        configCurrent+=this.getSecretKey();

        return configCurrent;
    }
}
